﻿using RentalDTO;
using RentalIService;
using RentalService.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace RentalService
{
    public class landlordTenantService : IServicelandlordTenant
    {

        /// <summary>
        /// 找回密码
        /// 账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<landlordTenantDTO> SelectOneList(string id)
        {
            using (var db = new RentalContext())
            {
                BaseService<landlordTenant> bs = new BaseService<landlordTenant>(db);

                Expression<Func<landlordTenant, bool>> where;
                if (id != "")
                {
                    where = u => u.Id == id;
                }
                else
                {
                    where = u => true;
                }


                var list = bs.GetLists(where).Select(u => new landlordTenantDTO
                {
                    Id = u.Id,
                    Name = u.Name
                }).ToList();
                return list;
            }
        }

        public bool Add(string name, int sex, DateTime birthDay, string nativePlace, string address, string tel, string cardId, string password, string email, string roleId)
        {
            using (var db = new RentalContext())
            {
                //随机数
                Random ran = new Random();

                var list = SelectOneList("");
                string id = "F" + list.Count;

                if (SelectOneList(id).Count == 1)
                {
                    id += ran.Next(10);
                }
                BaseService<landlordTenant> bs = new BaseService<landlordTenant>(db);

                landlordTenant land = new landlordTenant();

                land.Id = id;
                land.Name = name;
                land.Sex = sex;
                land.BirthDay = birthDay;
                land.NativePlace = nativePlace;
                land.Address = address;
                land.Tel = tel;
                land.cardId = cardId;
                land.CreateTime = DateTime.Now;
                land.Password = password;
                land.Email = email;
                land.RoleId = roleId;
                land.IsDel = 1;

                bool b = bs.Add(land);
                return b;

            }
        }

        /// <summary>
        /// 软删
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool RDel(List<string> ids)
        {
            using (var db = new RentalContext())
            {
                foreach (var item in ids)
                {
                    var dels = db.landlordTenant.SingleOrDefault(e => e.Id == item);
                    dels.IsDel = 0;
                }
                int m = db.SaveChanges();
                return m > 0;
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="limit"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <param name="name"></param>
        /// <param name="rowCount"></param>
        /// <returns></returns>
        public List<landlordTenantDTO> Selectlanlord(int offset, int limit, string sort, string order, string name, ref int rowCount)
        {
            using (var db = new RentalContext())
            {
                BaseService<landlordTenant> bs = new BaseService<landlordTenant>(db);
                rowCount = 0;
                Expression<Func<landlordTenant, bool>> where = a => a.IsDel == 1;

                if (!String.IsNullOrWhiteSpace(name))
                {
                    where = where.And(r => r.Name.Contains(name));
                }
                var orderBy = Common.PredicateExtensions.GetSortLambda<landlordTenant>(sort);

                bool orderType = order == "asc" ? true : false;

                var list = bs.GetPagedList(offset, limit, ref rowCount, where, orderBy, orderType).ToList().Select(m => new landlordTenantDTO
                {
                    Id = m.Id,
                    Name = m.Name,
                    Sex = m.Sex,
                    BirthDay = m.BirthDay,
                    NativePlace = m.NativePlace,
                    Address=m.Address,
                    cardId = m.cardId,
                    Tel = m.Tel,
                    Password=m.Password,
                    Email=m.Email,
                    Remark = m.Remark,
                    BirthDay111 = m.BirthDay.ToString("yyyy-MM-dd")
                }).ToList();
                return list;
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public List<landlordTenantDTO> SelectOneList(string id, string pwd)
        {
            using (var db = new RentalContext())
            {
                BaseService<landlordTenant> bs = new BaseService<landlordTenant>(db);

                Expression<Func<landlordTenant, bool>> where = u => u.Id == id && u.Password == pwd;

                var list = bs.GetLists(where).Select(u => new landlordTenantDTO
                {
                    Id = u.Id,
                    Name = u.Name,
                    RoleId=u.RoleId
                }).ToList();
                return list;
            }
        }

        public bool Edit(landlordTenantDTO model)
        {
            using (var db = new RentalContext())
            {
                landlordTenant land = new landlordTenant();
                land.Id = model.Id;
                land.Name = model.Name;
                land.BirthDay = model.BirthDay;
                land.NativePlace = model.NativePlace;
                land.Address = model.Address;
                land.Tel = model.Tel;
                land.cardId = model.cardId;
                land.Password = model.Password;
                land.Email = model.Email;
                land.RoleId = model.RoleId;
                land.UpdateTime= DateTime.Now;

                BaseService<landlordTenant> bs = new BaseService<landlordTenant>(db);
                bool m = bs.Update(land);
                return m;

            }
        }
    }
}
